import 'dart:io';
import 'package:protoo_client/protoo_client.dart';
import 'package:protoo_client/src/transports/NativeTransport.dart'
  if (dart.library.html) 'package:protoo_client/src/transports/WebTransport.dart';

final url = 'wss://v3demo.mediasoup.org:4443';
final roomId = 'asdasdds';
final peerId = 'zxcvvczx';

main() async {
  try {
    Peer peer = new Peer(Transport('ws://192.168.18.120:7000/?code=10005&version=0.0.2'));

    peer.on('open', () {
      print('open');

      peer.request('call.start', null).then((data) {
        print('call.start response: ' + data.toString());
      }).catchError((error) {
        print('call.start response error: ' + error.toString());
      });

      sleep(Duration(seconds: 1));

      /*const timeout = Duration(seconds: 1);
      Timer.periodic(timeout, (timer) {
        if(!peer.connected){
          print('ping unconnected');
          return;
        }
        peer.request('ping', {'t': DateTime.now().millisecondsSinceEpoch}).then((data) {
          print('ping response: ' + data.toString());
        }).catchError((error) {
          print('ping response error: ' + error.toString());
        });
      });*/
    });

    peer.on('close', () {
      print('close');
    });

    peer.on('failed', (error) {
      print("peer failed!");
      print(error);
    });

    peer.on('request', (request, accept, reject) {
      print('request: ' + request.toString());
      if(request["method"] == "ping"){
        try {
          accept("pong");
          return;
        }catch(e){
          print(e);
          return;
        }
      }
      accept({'key1': "value1", 'key2': "value2"});
    });

    peer.on('notification', (notification) {
      print('notification: ' + notification.toString());
    });

  } catch (e) {
    print('Client init "error" event');
  }
}
